//
//  System_ScreenInfoHandler.swift
//  mall-ios
//
//  Created by Meak Tsui on 2018/11/28.
//  Copyright © 2018 Meak Tsui. All rights reserved.
//

import Foundation
import UIKit
import MTHybrid

extension Api {
    static let ScreenInfo = Api("screenInfo")
}

class System_ScreenInfoHandler: MTEventHandler {
    var ns: Ns {
        return .System
    }
    
    var action: Api {
        return .ScreenInfo
    }
    
    let Landscape = 1 //横屏
    let Portrait = 0  //竖屏
    
    func handler(_ msg: MTHybridMsg) {
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        let orientation = UIDevice.current.orientation
        UIDevice.current.endGeneratingDeviceOrientationNotifications()
        var orientate = Portrait
        switch orientation {
        case .landscapeLeft:
            orientate = Landscape
        case .landscapeRight:
            orientate = Landscape
        default:
            orientate = Portrait
        }
        let bounds = UIScreen.main.bounds
        msg.callbackClosure?(["orientate":orientate,"width":bounds.width,"height":bounds.height])
    }
}
